package com.ziyun.erp.web.modules.goods.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.plugins.Page;
import com.netflix.discovery.converters.Auto;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.goods.entity.GoodsOptionEntity;
import com.ziyun.erp.modules.goods.service.GoodsOptionService;
import com.ziyun.erp.modules.goodsCategory.entity.GoodsCategoryEntity;
import com.ziyun.erp.modules.goodsCategory.service.GoodsCategoryService;
import com.ziyun.erp.modules.message.entity.MessageEntity;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.goods.entity.GoodsAttributeEntity;
import com.ziyun.erp.modules.goods.service.GoodsAttributeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 商品属性表
 *
 * @author
 * @email
 * @date 2018-08-20 09:57:04
 */
@RestController
@RequestMapping("goods/goodsattribute")
public class GoodsAttributeController {
    @Autowired
    private GoodsAttributeService goodsAttributeService;
    @Autowired
    private GoodsCategoryService goodsCategoryService;
    @Autowired
    private GoodsOptionService goodsOptionService;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("goods:goodsattribute:list")
    public R list(Page<GoodsAttributeEntity> page, @RequestParam Map<String, Object> params){
//        PageUtils page = goodsAttributeService.queryPage(params);
        page.setSize(Integer.parseInt((String) params.get("limit")));
        page.setCurrent(Integer.parseInt((String) params.get("page")));
        Page<GoodsAttributeEntity> resultPage = goodsAttributeService.queryNewPage(page, params);
        return R.ok().put("page", resultPage);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attributeId}")
    @RequiresPermissions("goods:goodsattribute:info")
    public R info(@PathVariable("attributeId") Long attributeId){
        GoodsAttributeEntity goodsAttribute = goodsAttributeService.selectById(attributeId);
        GoodsCategoryEntity goodsCategory = goodsCategoryService.selectById(goodsAttribute.getCategoryId());
        return R.ok().put("goodsAttribute", goodsAttribute).put("goodsCategory",goodsCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goods:goodsattribute:save")
    public R save(@RequestBody GoodsAttributeEntity goodsAttribute){
        goodsAttribute.setCreateName(UserUtils.getCurentUser().getUsername());
        goodsAttributeService.saveAttriute(goodsAttribute);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("goods:goodsattribute:update")
    public R update(@RequestBody GoodsAttributeEntity goodsAttribute){
        ValidatorUtils.validateEntity(goodsAttribute);
        goodsAttributeService.updateGoodsAttribute(goodsAttribute,UserUtils.getCurentUser().getUserId());
//        goodsAttributeService.updateAllColumnById(goodsAttribute);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goods:goodsattribute:delete")
    public R delete(@RequestBody Long[] attributeIds){
//        goodsAttributeService.deleteBatchIds(Arrays.asList(attributeIds));
        //标记删除规格属性
        goodsAttributeService.fagDelAttribute(Arrays.asList(attributeIds));
        return R.ok();
    }
    /**
     *获得所有的规格属性,供商品模板选择规格属性使用
     */
    @RequestMapping("/getTempGoodsAttribute/{categoryId}")
    public R getTempGoodsAttribute(@PathVariable("categoryId") Integer categoryId){
        List<GoodsAttributeEntity> attributeEntityList = goodsAttributeService.getTemplateAttribute(categoryId);
        return R.ok().put("attributeEntityList",attributeEntityList);
    }

    /**
     * 信息
     */
    @RequestMapping("/getAttributesByTemplateId/{templateId}")
    public R getAttributesByTemplateId(@PathVariable("templateId") Integer templateId){
//        List<GoodsAttributeEntity> attributeList = goodsAttributeService.getAttributesByTemplateId(templateId);
//        List<GoodsAttributeEntity> craftAttributeList = goodsAttributeService.getCraftAttributesByTemplateId(templateId);
        return goodsAttributeService.getAttributesByTemplateId(templateId,1L);
    }

    /**
     * 信息
     */
    @RequestMapping("/getCraftAttributesByTemplateId/{templateId}")
    public R getCraftAttributesByTemplateId(@PathVariable("templateId") Integer templateId){
        List<GoodsAttributeEntity> craftAttributeList = goodsAttributeService.getCraftAttributesByTemplateId(templateId);
        return R.ok().put("craftAttributeList",craftAttributeList);
    }

    /**
     * 生成私有选项
     */
    @RequestMapping("/newOption")
    public R newOption(@RequestBody GoodsOptionEntity goodsOptionEntity){
        goodsOptionEntity.setSellerId(1L);
        goodsOptionEntity.setCreateName(UserUtils.getCurentUser().getUsername());
        goodsOptionService.insert(goodsOptionEntity);
        return R.ok().put("goodsOptionEntity",goodsOptionEntity);
    }

}
